package com.findviewbydream.evil.eda.View;

import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AbsListView;

/********************************************
 * 工程名：Eda
 * 包名：com.findviewbydream.evil.eda.View
 * 时间：16-9-2 下午3:11
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：防止下拉刷新与ListView冲突
 ********************************************/
public class SwpipeListViewOnScrollListener implements AbsListView.OnScrollListener
{

    private SwipeRefreshLayout mSwipeView;
    private AbsListView.OnScrollListener mOnScrollListener;

    public SwpipeListViewOnScrollListener(SwipeRefreshLayout swipeView)
    {
        mSwipeView = swipeView;
    }

    public SwpipeListViewOnScrollListener(SwipeRefreshLayout swipeView,
                                          AbsListView.OnScrollListener onScrollListener)
    {
        mSwipeView = swipeView;
        mOnScrollListener = onScrollListener;
    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int i)
    {
    }

    @Override
    public void onScroll(AbsListView absListView, int firstVisibleItem,
                         int visibleItemCount, int totalItemCount)
    {
        View firstView = absListView.getChildAt(firstVisibleItem);

        // 当firstVisibleItem是第0位。如果firstView==null说明列表为空，需要刷新;或者top==0说明已经到达列表顶部, 也需要刷新
        if (firstVisibleItem == 0 && (firstView == null || firstView.getTop() == 0))
        {
            mSwipeView.setEnabled(true);
        }
        else
        {
            mSwipeView.setEnabled(false);
        }
        if (null != mOnScrollListener)
        {
            mOnScrollListener.onScroll(absListView, firstVisibleItem,
                    visibleItemCount, totalItemCount);
        }
    }
}